﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace TTBox
{
    public partial class SReportPanel : UserControl
    {

        public SReportPanel()
        {
            InitializeComponent();
        }

        int m_nIndex = 0;
        string[] m_straryItem;
        public void initial()
        {
            this.panel_Confirm.Visible = false;
            this.Tag = "";
            m_nIndex = 0;
            this.button_UpPage.Visible = false;
            this.button_DownPage.Visible = true;
            m_straryItem = StoreData.getInstance().SReportMenu.Split(';');

            setupButtons();

        }

        private void setupButtons()
        {
            button_Item1.Visible = false;
            button_Item2.Visible = false;
            button_Item3.Visible = false;
            button_Item4.Visible = false;
            button_Item5.Visible = false;
            button_Item6.Visible = false;

            if(m_nIndex < m_straryItem.Length) setupButton(button_Item1, m_nIndex);
            if (m_nIndex+1 < m_straryItem.Length) setupButton(button_Item2, m_nIndex + 1);
            if (m_nIndex + 2 < m_straryItem.Length) setupButton(button_Item3, m_nIndex + 2);
            if (m_nIndex + 3 < m_straryItem.Length) setupButton(button_Item4, m_nIndex + 3);
            if (m_nIndex + 4 < m_straryItem.Length) setupButton(button_Item5, m_nIndex + 4);
            if (m_nIndex + 5 < m_straryItem.Length) setupButton(button_Item6, m_nIndex + 5);
        }

        private void setupButton(Button arg_btn, int arg_nIndex)
        {
            string [] straryData = m_straryItem[arg_nIndex].Split('.');
            arg_btn.Text = straryData[1];
            arg_btn.Tag = straryData[0];
            arg_btn.Visible = true;
        }

        private void button_Item1_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;

            this.Tag = btn.Tag;
            this.panel_Confirm.Visible = true;
            this.label_ReportItem.Text = btn.Text;

            ButtonControl(false);
            

        }

        private void ButtonControl(bool arg_bEnabled)
        {
            foreach (Control c in this.Controls)
            {
                if (c is Button)
                {
                    c.Enabled = arg_bEnabled;
                }
            }

        }

        private void button_Return_Click(object sender, EventArgs e)
        {
            this.Tag = "";
            Form_Main.Main.ChangeToMainScreen();

        }

        private void button_Cancel_Click(object sender, EventArgs e)
        {
            this.Tag = "";
            this.panel_Confirm.Visible = false;
            ButtonControl(true);
        }

        private void button_OKSend_Click(object sender, EventArgs e)
        {
            this.panel_Confirm.Visible = false;

            string strResult = CmdTalker.getInstance().SendServerAndWaitResponse(Form_Main.Main.getCurrStatusChar() + this.Tag.ToString() + Form_Main.Main.getJobID(), 10, "MOK", true);

            if (strResult != "MOK")
            {
            }

            ButtonControl(true);
            Form_Main.Main.ChangeToMainScreen();
        }

        private void button_UpPage_Click(object sender, EventArgs e)
        {
            m_nIndex -= 6;
            this.button_UpPage.Visible = true;
            this.button_DownPage.Visible = true;
            if (m_nIndex <= 0)
            {
                this.button_UpPage.Visible = false;
                m_nIndex = 0;
            }
            setupButtons();
        }

        private void button_DownPage_Click(object sender, EventArgs e)
        {
            m_nIndex += 6;
            this.button_UpPage.Visible = true;
            this.button_DownPage.Visible = true;

            if (m_nIndex + 6 >= m_straryItem.Length)
            {
                this.button_DownPage.Visible = false;
            }
            setupButtons();
        }


    }
}
